package com.shop.controller;

import java.util.List;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.NzcpxlBean;
import com.shop.bean.ProPicsBean;
import com.shop.bean.ProductBean;

/**
 * 页面信息
 * @author bsh_2012@163.com
 *
 */
public class WelcomeContoller extends BaseController {

	//首页
	public void index(){
		// 首页图片 切换
		String sql = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 5";
		List<ProPicsBean> list = ProPicsBean.dao.find(sql,"101");//默认的 首页图片  五张切换
		setAttr("pclist",list);
		// 首页图片 三张列表
		String sql2 = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 3";
		List<ProPicsBean> list2 = ProPicsBean.dao.find(sql2,"102");//默认的 首页图片 三张列表
		setAttr("pclist2",list2);
		
		// 首页图片 热销商品
		String sql_hot = "select * from tblproduct tp where tp.pstate=1 ORDER BY tp.pfbtime DESC LIMIT 10";
		List<ProductBean> list_hot = ProductBean.productDao.find(sql_hot);//默认的 首页图片 三张列表
		setAttr("phot",list_hot);
		
		render("/index.jsp");
	}
	
	
	// 查看 商品详细页面
	public void gpro(){
		String pcode = getPara("pcode");
		String sql = "SELECT p.*,dl.dlname ,xl.xlname,dp.dpname from tblproduct p left join tblnzcpdl dl ON  dl.dlcode = p.pdlcode "+
					"left join tblnzcpxl xl ON  xl.xlcode = p.pxlcode "+
					"left join tbldpgl dp ON dp.dpcode = p.pdpcode where p.pcode =?";
		ProductBean pc = ProductBean.productDao.findFirst(sql, pcode);
		setAttr("p",pc);
		// 商品图片列表
		String sql_ = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 4";
		List<ProPicsBean> list = ProPicsBean.dao.find(sql_,pcode);
		setAttr("pclist",list);
		//评论列表
		String select = "select tbp.*,tbcd.dlname,tbcx.xlname,tbdg.dpname";
		String sqlExceptSelect = "from tblproduct tbp inner join tblnzcpdl tbcd on tbp.pdlcode=tbcd.dlcode"+
		" inner join tblnzcpxl tbcx on tbp.pxlcode=tbcx.xlcode inner join tbldpgl tbdg on tbp.pdpcode=tbdg.dpcode"+" where tbp.pdlcode=?";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
	//	Page<ProductBean> pps = ProductBean.productDao.paginate(page, 55, select,sqlExceptSelect,"101");
		Page<Record> pps = Db.paginate(page, 55, select,sqlExceptSelect,"101");
		setAttr("pps", pps.getList());
		  
		render(WIEW_WEB+"pro-detail.jsp");
	}
	
	
	// 商品列表 -- 根据大类查询列表
	public void glstdl(){
		String dlcode = getPara("dlcode");
		// 根据大类 查询列表   小类
		String sql_xl ="SELECT xl.*,dl.dlname from tblnzcpxl xl LEFT join tblnzcpdl dl on dl.dlcode = xl.dlcode where xl.dlcode = ?";
		List<NzcpxlBean> xls = NzcpxlBean.cpxlDao.find(sql_xl,dlcode);
		if(xls.size()>0){
			setAttr("dlname",xls.get(0).getStr("dlname"));
		}
		// 根据大类  列表商品信息
		String sql_hot = "select * from tblproduct tp where tp.pstate=1 ORDER BY tp.pfbtime DESC LIMIT 10";
		List<ProductBean> list_hot = ProductBean.productDao.find(sql_hot);//热卖 的 10件商品   4件 横向的，6件竖向的
		setAttr("phot",list_hot);
		
		// 列表
		String select = "select * ";
		String sqlExceptSelect = "from tblproduct tp where tp.pstate=1 ORDER BY tp.pfbtime DESC";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<ProductBean> ps = ProductBean.productDao.paginate(page, 16, select, sqlExceptSelect);
		setAttr("ps",ps.getList());
		setAttr("xls",xls);
		render(WIEW_WEB+"pro-list.jsp");
	}
	
	// 商品列表 -- 根据小类查询列表
		public void glstxl(){
			String dlcode = getPara("dlcode");
			String xlcode = getPara("xlcode");
			// 根据大类 查询列表
			String sql_xl ="SELECT xl.*,dl.dlname from tblnzcpxl xl LEFT join tblnzcpdl dl on dl.dlcode = xl.dlcode where xl.dlcode = ?";
			List<NzcpxlBean> xls = NzcpxlBean.cpxlDao.find(sql_xl,dlcode);
			if(xls.size()>0){
				setAttr("dlname",xls.get(0).getStr("dlname"));
			}
			// 根据小类类  列表商品信息
			
			setAttr("xls",xls);
			render(WIEW_WEB+"pro-list.jsp");
		}
		
	//宾馆预订
		public void hotel(){
			render(WIEW_WEB+"index_hotel.jsp");
		}
	// 贴心服务 
		public  void server(){
			String sql_xl ="SELECT xl.*,dl.dlname from tblnzcpxl xl LEFT join tblnzcpdl dl on dl.dlcode = xl.dlcode where xl.dlcode = ?";
			List<NzcpxlBean> xls = NzcpxlBean.cpxlDao.find(sql_xl,"101");// 贴心服务 code  101
			setAttr("xls", xls);
			
			String sql_hot = "select * from tblproduct tp where tp.pstate=1 ORDER BY tp.pfbtime DESC LIMIT 10";
			List<ProductBean> list_hot = ProductBean.productDao.find(sql_hot);//热卖 的  4件商品
			setAttr("phot",list_hot);
			
			render(WIEW_WEB+"index_server.jsp");
		}
}
